package com.darren.euc.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.fasterxml.jackson.annotation.JsonFormat;

@Entity
@Table(name = "euc_contracts")
public class Contract implements Serializable {

	private static final long serialVersionUID = 7225410464606595171L;

	@Id
	@Column(name = "id")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;
	
	/**
	 * 合同类型：入驻合同，退租合同
	 */
	@Column(name = "contract_type")
	private String contractType;
	
	
	/**
	 * 合同起始时间
	 */
	@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
	@Column(name = "contract_start_time",nullable=true,columnDefinition="datetime")
	private Date contractStartTime;
	
	/**
	 * 合同结束时间
	 */
	@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
	@Column(name = "contract_end_time",nullable=true,columnDefinition="datetime")
	private Date contractEndTime;
	
	/**
	 * 订单
	 */
	@ManyToOne  
    @JoinColumn(name = "order_id")  
	private Order order;
	
	/**
	 * 合同所包含的产品
	 */
	@OneToMany(mappedBy = "contract", cascade = CascadeType.ALL, fetch = FetchType.LAZY)  
	private List<ContractProduct> contractProductList = new ArrayList<ContractProduct>();
	
	/**
	 * 合同所包含的分期
	 */
	@OneToMany(mappedBy = "contract", cascade = CascadeType.ALL, fetch = FetchType.LAZY)  
	private List<ContractHirePurchase> contractHirePurchaseList = new ArrayList<ContractHirePurchase>();
	
	/**
	 * 承租方（甲方）
	 */
	@Column(name = "partyA_name")
	private String partyAName;

	/**
	 * 承租方（甲方）  联系人
	 */
	@Column(name = "PartyA_contact")
	private String partyAContact;
	
	/**
	 * 承租方（甲方）  联系电话
	 */
	@Column(name = "partyA_contact_number")
	private String partyAContactNumber;
	
	/**
	 * 承租方（甲方）  地址
	 */
	@Column(name = "partyA_address")
	private String partyAAddress;
	
	/**
	 * 承租方（甲方）  邮件
	 */
	@Column(name = "partyA_email")
	private String partyAEmail;
	
	/**
	 * 出租方（乙方）
	 */
	@Column(name = "partyB_name")
	private String partyBName;

	/**
	 * 出租方（乙方）  联系人
	 */
	@Column(name = "partyB_contact")
	private String partyBContact;
	
	/**
	 * 出租方（乙方）  联系电话
	 */
	@Column(name = "partyB_contact_number")
	private String partyBContactNumber;
	
	/**
	 * 出租方（乙方）  地址
	 */
	@Column(name = "partyB_address")
	private String partyBAddress;
	
	/**
	 * 出租方（乙方）  邮件
	 */
	@Column(name = "partyB_email")
	private String partyBEmail;
	
	/**
	 * 签约时间
	 */
	@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
	@Column(name = "contract_time",nullable=true,columnDefinition="datetime")
	private Date contractTime;
	
	/**
	 * 签约城市
	 */
	@Column(name = "contract_city", length = 20)
	private String contractCity;
	
	/**
	 * 合同编号
	 */
	@Column(name = "contract_number")
	private String contractNumber;
	
	
	/**
	 * 合同金额
	 */
	@Column(name = "contract_amount", nullable = true)
	private Double contractAmount;
	
	
	/**
	 * 分期方式
	 */
	@Column(name = "payment_count", nullable = true)
	private Integer paymentCount;
	
	
	/**
	 * 押金总额
	 */
	@Column(name = "deposit_amount", nullable = true)
	private Double depositAmount;
	
	
	/**
	 * 一期金额
	 */
	@Column(name = "payment_first", nullable = true)
	private Double paymentFirst;
	
	/**
	 * 一期付款日期
	 */
	@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
	@Column(name = "payment_first_time",nullable=true,columnDefinition="datetime")
	private Date paymentFirstTime;
	
	
	/**
	 * 免租期 开始时间
	 */
	@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
	@Column(name = "free_time_start",nullable=true,columnDefinition="datetime")
	private Date freeTimeStart;
	
	
	/**
	 * 免租期 结束时间
	 */
	@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
	@Column(name = "free_time_end",nullable=true,columnDefinition="datetime")
	private Date freeTimeEnd;
	
	
	
	/**
	 * 退租日期
	 */
	@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
	@Column(name = "leave_time",nullable=true,columnDefinition="datetime")
	private Date leaveTime;
	
	

	/**
	 * 退服务费金额
	 */
	@Column(name = "back_amount", nullable = true)
	private Double backAmount;
	
	
	/**
	 * 退押金总额
	 */
	@Column(name = "back_deposit_amount", nullable = true)
	private Double backDepositAmount;	
	
	/**
	 * 合同文件路径
	 */
	@Column(name = "file_path")
	private String filePath;
	
	
	/**
	 * 合同描述信息，备用
	 */
	@Column(name = "description")
	private String description;
	
	
	@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
	@Column(name = "create_time",nullable=true,columnDefinition="datetime")
	private Date createTime;

	/**
	 * 销售员
	 */
	@ManyToOne(targetEntity = User.class, fetch = FetchType.EAGER)
	@JoinColumn(name = "create_user_id", referencedColumnName = "user_id")
	private User createUser;
	
	@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
	@Column(name = "last_modify_time",nullable=true,columnDefinition="datetime")
	private Date lastModifyTime;
	
	@Column(name = "last_modify_user_id")
	private Integer lastModifyUserId;
	
	@Column(name = "months")
	private Integer months = 0;
	
	@Column(name = "enable")
	private boolean enable = true;

	@Transient
	private Integer roomCount = 0;	
	
	/**
	 * 付款期限，付款日到之后，必须在15天内完成付款
	 */
	@Transient
	private Integer depositPeriod = 15;	

	@Transient
	private Double averagePrice = 0D;
	
	@Transient
	private User salesPerson;
	
	@Transient
	private String clientName;
	
	
	@Transient
	private Integer spaceId;
	
	
	public Integer getRoomCount() {
		roomCount = 0;
		if(contractProductList != null && contractProductList.size() > 0){			
			roomCount = contractProductList.size();			
		}		
		return roomCount;
	}


	public void setRoomCount(Integer roomCount) {
		this.roomCount = roomCount;
	}

	

	public Integer getDepositPeriod() {
		return depositPeriod == null?15:depositPeriod;
	}


	public void setDepositPeriod(Integer depositPeriod) {
		this.depositPeriod = depositPeriod;
	}


	public Double getAveragePrice() {
		averagePrice = 0D;
		if(contractAmount != null && contractAmount > 0 && getRoomCount() > 0){
			averagePrice = contractAmount/getRoomCount()/months;
		}
		averagePrice = (double) (Math.round(averagePrice*100)/100.00);
		return averagePrice;
	}


	public void setAveragePrice(Double averagePrice) {
		this.averagePrice = averagePrice;
	}


	public Integer getId() {
		return id;
	}


	public void setId(Integer id) {
		this.id = id;
	}


	public String getContractType() {
		return contractType;
	}


	public void setContractType(String contractType) {
		this.contractType = contractType;
	}


	public Date getContractStartTime() {
		return contractStartTime;
	}


	public void setContractStartTime(Date contractStartTime) {
		this.contractStartTime = contractStartTime;
	}


	public Date getContractEndTime() {
		return contractEndTime;
	}


	public void setContractEndTime(Date contractEndTime) {
		this.contractEndTime = contractEndTime;
	}


	public Order getOrder() {
		return null;//order;
	}


	public void setOrder(Order order) {
		this.order = order;
	}

	
	

	public User getSalesPerson() {
		if(order != null){
			setSalesPerson(order.getSalesPerson());
		}
		return salesPerson;
	}


	public void setSalesPerson(User salesPerson) {
		this.salesPerson = salesPerson;
	}

	public String getClientName() {
		if(order != null ){
			setClientName(order.getClientName());
		}
		return clientName;
	}


	public void setClientName(String clientName) {
		this.clientName = clientName;
	}

	

	public Integer getSpaceId() {
		if(order != null ){
			setSpaceId(order.getSpaceId());
		}
		return spaceId;
	}


	public void setSpaceId(Integer spaceId) {
		this.spaceId = spaceId;
	}


	public List<ContractProduct> getContractProductList() {
		return contractProductList;
	}


	public void setContractProductList(List<ContractProduct> contractProductList) {
		this.contractProductList = contractProductList;
	}


	public List<ContractHirePurchase> getContractHirePurchaseList() {
		if(contractHirePurchaseList == null){
			contractHirePurchaseList = new ArrayList<ContractHirePurchase>();
		}
		return contractHirePurchaseList;
	}


	public void setContractHirePurchaseList(List<ContractHirePurchase> contractHirePurchaseList) {
		this.contractHirePurchaseList = contractHirePurchaseList;
	}


	public String getPartyAName() {
		return partyAName;
	}


	public void setPartyAName(String partyAName) {
		this.partyAName = partyAName;
	}


	public String getPartyAContact() {
		return partyAContact;
	}


	public void setPartyAContact(String partyAContact) {
		this.partyAContact = partyAContact;
	}


	public String getPartyAContactNumber() {
		return partyAContactNumber;
	}


	public void setPartyAContactNumber(String partyAContactNumber) {
		this.partyAContactNumber = partyAContactNumber;
	}


	public String getPartyAAddress() {
		return partyAAddress;
	}


	public void setPartyAAddress(String partyAAddress) {
		this.partyAAddress = partyAAddress;
	}


	public String getPartyAEmail() {
		return partyAEmail;
	}


	public void setPartyAEmail(String partyAEmail) {
		this.partyAEmail = partyAEmail;
	}


	public String getPartyBName() {
		return partyBName;
	}


	public void setPartyBName(String partyBName) {
		this.partyBName = partyBName;
	}


	public String getPartyBContact() {
		return partyBContact;
	}


	public void setPartyBContact(String partyBContact) {
		this.partyBContact = partyBContact;
	}


	public String getPartyBContactNumber() {
		return partyBContactNumber;
	}


	public void setPartyBContactNumber(String partyBContactNumber) {
		this.partyBContactNumber = partyBContactNumber;
	}


	public String getPartyBAddress() {
		return partyBAddress;
	}


	public void setPartyBAddress(String partyBAddress) {
		this.partyBAddress = partyBAddress;
	}


	public String getPartyBEmail() {
		return partyBEmail;
	}


	public void setPartyBEmail(String partyBEmail) {
		this.partyBEmail = partyBEmail;
	}


	public Date getContractTime() {
		return contractTime;
	}


	public void setContractTime(Date contractTime) {
		this.contractTime = contractTime;
	}


	public String getContractCity() {
		return contractCity;
	}


	public void setContractCity(String contractCity) {
		this.contractCity = contractCity;
	}


	public String getContractNumber() {
		return contractNumber;
	}


	public void setContractNumber(String contractNumber) {
		this.contractNumber = contractNumber;
	}


	public Double getContractAmount() {
//		if(contractProductList != null && contractProductList.size() > 0){			
//			contractAmount = 0D;			
//			for(ContractProduct contractProduct:contractProductList){				
//				contractAmount += contractProduct.getRoomPrice() == null?0:contractProduct.getRoomPrice();				
//			}
//			contractAmount = contractAmount * months;
//		}		
		return contractAmount;
	}


	public void setContractAmount(Double contractAmount) {
		this.contractAmount = contractAmount;
	}


	public Integer getPaymentCount() {
		return paymentCount == null?0:paymentCount;
	}


	public void setPaymentCount(Integer paymentCount) {
		this.paymentCount = paymentCount;
	}


	public Double getDepositAmount() {
		return depositAmount;
	}


	public void setDepositAmount(Double depositAmount) {
		this.depositAmount = depositAmount;
	}


	public Double getPaymentFirst() {
		return paymentFirst;
	}


	public void setPaymentFirst(Double paymentFirst) {
		this.paymentFirst = paymentFirst;
	}


	public Date getPaymentFirstTime() {
		return paymentFirstTime;
	}


	public void setPaymentFirstTime(Date paymentFirstTime) {
		this.paymentFirstTime = paymentFirstTime;
	}


	public Date getFreeTimeStart() {
		return freeTimeStart;
	}


	public void setFreeTimeStart(Date freeTimeStart) {
		this.freeTimeStart = freeTimeStart;
	}


	public Date getFreeTimeEnd() {
		return freeTimeEnd;
	}


	public void setFreeTimeEnd(Date freeTimeEnd) {
		this.freeTimeEnd = freeTimeEnd;
	}


	public Date getLeaveTime() {
		return leaveTime;
	}


	public void setLeaveTime(Date leaveTime) {
		this.leaveTime = leaveTime;
	}


	public Double getBackAmount() {
		return backAmount;
	}


	public void setBackAmount(Double backAmount) {
		this.backAmount = backAmount;
	}


	public Double getBackDepositAmount() {
		return backDepositAmount;
	}


	public void setBackDepositAmount(Double backDepositAmount) {
		this.backDepositAmount = backDepositAmount;
	}


	public String getFilePath() {
		return filePath;
	}


	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}


	public String getDescription() {
		return description;
	}


	public void setDescription(String description) {
		this.description = description;
	}


	public Date getCreateTime() {
		return createTime;
	}


	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public User getCreateUser() {
		return createUser;
	}


	public void setCreateUser(User createUser) {
		this.createUser = createUser;
	}


	public Date getLastModifyTime() {
		return lastModifyTime;
	}


	public void setLastModifyTime(Date lastModifyTime) {
		this.lastModifyTime = lastModifyTime;
	}


	public Integer getLastModifyUserId() {
		return lastModifyUserId;
	}


	public void setLastModifyUserId(Integer lastModifyUserId) {
		this.lastModifyUserId = lastModifyUserId;
	}

	

	public Integer getMonths() {
		return months;
	}


	public void setMonths(Integer months) {
		this.months = months;
	}


	public boolean isEnable() {
		return enable;
	}


	public void setEnable(boolean enable) {
		this.enable = enable;
	}

}
